package com.neusoft.EYSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.EYSystem.dao.NursingLevelDAO;
import com.neusoft.EYSystem.po.NursingLevel;
import com.neusoft.EYSystem.po.NursingProject;
import com.neusoft.EYSystem.po.PageResponseBean;
import com.neusoft.EYSystem.po.ResponseBean;
import com.neusoft.EYSystem.service.NursingLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nursingLevel")
public class NursingLevelController {

    // ✅ 工具方法：判断是否为合法字符串（排除 null、空串、"null"）
    private boolean isValid(String value) {
        return value != null && !value.trim().isEmpty() && !"null".equalsIgnoreCase(value.trim());
    }

    @Autowired
    private NursingLevelService nursingLevelService;

    @Autowired
    private NursingLevelDAO nursingLevelDAO;

    // 管理员端：护理级别 护理级别表格显示
    // 支持根据levelId，levelName，status查询
    // 多个查询条件，可封装成实体类结合分页查询
    @GetMapping("/pageNursingLevel")
    public PageResponseBean<List<NursingLevel>> pageNursingLevel(Long cur, Long number,NursingLevel nursingLevel) {
       return nursingLevelService.pageNursingLevel(cur, number, nursingLevel);
    }

    // 管理员端：护理级别 弹窗添加护理级别
    @PostMapping("/addNursingLevel")
    public ResponseBean<Integer> addNursingLevel(NursingLevel nursingLevel) {
        ResponseBean<Integer> response = new ResponseBean<>();
        try {
            if(nursingLevelDAO.insert(nursingLevel) > 0){
                response.setStatus(200);
                response.setMsg("护理级别添加成功");
                return response;
            }else {
                response.setStatus(400);
                response.setMsg("护理级别添加失败，可能是数据不完整或重复");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("护理级别添加失败: " + e.getMessage());
        }
        return response;
    }

    // 管理员端：护理级别 护理级别表格按钮逻辑删除护理级别
    @PostMapping("/deleteNursingLevel")
    public ResponseBean<Integer> deleteNursingLevel(NursingLevel nursingLevel) {
        Integer result;
        ResponseBean<Integer> rb = null;
        try {
            result = nursingLevelService.deleteStatusById(nursingLevel);
            rb =new ResponseBean<>(result);

        } catch (Exception e) {
            e.printStackTrace();
            rb =new ResponseBean<>(500,"删除护理级别错误");
        }
        return rb;
    }

    // 管理员端：护理级别 弹窗修改护理级别
    @PostMapping("/updateNursingLevel")
    public ResponseBean<Integer> updateNursingLevel(NursingLevel nursingLevel) {
        Integer result;
        ResponseBean<Integer> rb = null;
        UpdateWrapper<NursingLevel> uw = new UpdateWrapper<>();
        uw.eq("level_id", nursingLevel.getLevelId());
        try{
            result = nursingLevelDAO.update(nursingLevel, uw);
            rb = new ResponseBean<>(result);
        }catch (Exception e){
            e.printStackTrace();
            rb =new ResponseBean<>(500,"修改护理级别错误");
        }
        return rb;
    }

}
